package com.yupi.project.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.model.entity.InterfaceInfo;
import com.mdd.model.entity.UserInterfaceInfo;
import com.mdd.service.InnerInterfaceInfoService;
import com.mdd.service.InnerUserInterfaceInfoService;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.exception.ThrowUtils;
import com.yupi.project.mapper.InterfaceInfoMapper;
import com.yupi.project.service.UserInterfaceInfoService;
import com.yupi.project.service.impl.UserInterfaceInfoServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author Mdd
 * @Github <a href="https://github.com/mdd123cc"/>
 * @Gitee <a href="https://gitee.com/mdd115192"/>
 */
@DubboService
public class InnerUserInterfaceServiceImpl  implements InnerUserInterfaceInfoService {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
    }

    @Override
    public boolean invokeCount(long userId, long interfaceId) {
        return userInterfaceInfoService.invokeCount(userId,interfaceId);
    }

    @Override
    public boolean judgeInvokeCount(long userId, long interfaceId) {
        LambdaUpdateWrapper<UserInterfaceInfo> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(UserInterfaceInfo::getUserId, userId).eq(UserInterfaceInfo::getInterfaceInfoId, interfaceId);
        UserInterfaceInfo userInterfaceInfo;
        try {
            userInterfaceInfo = userInterfaceInfoService.getOne(queryWrapper);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "记录有多条!");
        }
        Integer leftNum = userInterfaceInfo.getLeftNum();
        return leftNum > 0;
    }
}
